﻿using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

namespace CWM.Skinn.FFD
{
    [CustomEditor(typeof(LatticeDeformer))]
    [DisallowMultipleComponent]
    public class LatticeDeformerInspector : Editor
    {
       
        [MenuItem("CONTEXT/LatticeDeformer/New Lattice", false, 98)]
        public static void CreateFFDMeshFFDVisualization(MenuCommand command)
        {
            var context = (LatticeDeformer)command.context;
            var ffd = LatticeDeformer.CreateFFD(context); if (!ffd) return;
            Undo.RegisterCreatedObjectUndo(ffd, "New Lattice");
            Selection.activeGameObject = ffd.gameObject;
            SkinnEx.ReleaseWithUndo(context.gameObject);
        }

        public static Editor Inspector { get; private set; }
        [MenuItem("CONTEXT/LatticeDeformer/Edit Inspector Script", false, 10)]
        public static void Open(MenuCommand command) { if (Inspector) AssetDatabase.OpenAsset(MonoScript.FromScriptableObject(Inspector), 0); }
        public void OnEnable() { Inspector = this; }

        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();

            var t = target as LatticeDeformer;

            EditorGUILayout.LabelField(" ");
            EditorGUILayout.LabelField("");

            if (GUI.Button(GUILayoutUtility.GetLastRect(), "New Lattice"))
            {
                var context = t;
                var ffd = LatticeDeformer.CreateFFD(context); if (!ffd) return;
                Undo.RegisterCreatedObjectUndo(ffd, "New Lattice");
                Selection.activeGameObject = ffd.gameObject;
                SkinnEx.ReleaseWithUndo(context.gameObject);
                return;
            }

            EditorGUILayout.LabelField("");

            EditorGUILayout.LabelField("drag select in the scene view to select lattice points.", EditorStyles.helpBox);
        }
    }
}